﻿
// http://www.codeproject.com/Articles/424642/Customer-KnockoutJS-and-MVC-demo-using-JSON


DeixeiDomain.PortfolioIndexViewModel = function () {
    var self = this;

    self.List = ko.observableArray([]);
    self.SelectedListItem = ko.observable();

    self.Item = ko.observable(new DeixeiDomain.NavigationArea({ GroupName: "Areas", Area: "Area 32", Description: "Nao sei bem", Url: "um url bacana!" }));
    self.WebApp = ko.observable(new DeixeiDomain.WebAppPortfolio({ Name: "Application Name", Description: "Application Name Description", Url: "http://localhost:9999/" }));

    self.WebAppList = ko.observableArray([]);
    self.WebAppListValue = ko.observable();

    self.PageInfo = ko.observable(new DeixeiDomain.PagedResult({
        TotalPages: 0,
        TotalCount: 0,
        PageIndex: 1,
        PageSize: 10,
        HasPreviousPage: false,
        HasNextPage: false,
        StartPage: 1,
        EndPage: 1,
        NextPage: 1,
        PreviousPage: 1
    }));

    self.AddApplication = function () {
        if ($("#AddWebApplicationDialogBoxForm").valid()) {
            amplify.request("AddWebAppPortfolio", self.WebApp(), function (result) {
                toastr.success("New Web Application added", "Add!");
                self.GetWebAppPortfolio();
                $("#AddWebApplicationDialogBox").modal("hide");
            });
        }
        else {
            toastr.error("Invalid Form", "Please read the form again, correct any errors!");
        }
    };

    self.AddNavigationArea = function () {
        if ($("#MyForm").valid()) {
            var seletedApp = self.WebAppListValue();
            self.Item().WebAppPortfolio(seletedApp);

            amplify.request("AddNavigationArea", self.Item(), function (result) {
                toastr.success("New Area data received", "Success Information!");
                self.PageInfo().PageIndex(self.PageInfo().EndPage());
                self.GetNavigationAreasByPage();
            });
        }
        else {
            toastr.error("Invalid Form", "Please read the form again, correct any errors!");
        }
    };

    self.EditNavigationArea = function (data) {
        self.Item(data);
    };

    self.SaveNavigationArea = function (data) {
        if ($("#MyForm").valid()) {
            amplify.request("EditNavigationArea", self.Item(), function (result) {
                toastr.success("Area data Saved", "Save!");
                self.GetNavigationAreasByPage();
            });
        }
        else {
            toastr.error("Invalid Form", "Please read the form again, correct any errors!");
        }
    };


    self.DeleteNavigationArea = function (data) {

        amplify.request("DeleteNavigationArea", { id: data.Id }, function (result) {
            self.GetNavigationAreasByPage();
            toastr.success("Area " + result.Area + " #" + result.Id +  " deleted", "Delete");
        });

        
    };

    self.GetNavigationAreasByPage = function () {

        var index = self.PageInfo().PageIndex();
        var pageSize = self.PageInfo().PageSize();
        var webAppListValue = self.WebAppListValue();
        var webAppId = 0;
        if (webAppListValue != null)
        {
            webAppId = webAppListValue.Id;
        }

        var paging = { ApplicationId: webAppId, index: index, pageSize: pageSize };

        amplify.request("GetNavigationAreasByPage", paging, function (result) {
            self.List(result.ContentList);

            self.PageInfo().TotalPages(result.TotalPages);
            self.PageInfo().TotalCount(result.TotalCount);
            self.PageInfo().PageIndex(result.PageIndex);
            self.PageInfo().PageSize(result.PageSize);
            self.PageInfo().HasPreviousPage(result.HasPreviousPage);
            self.PageInfo().HasNextPage(result.HasNextPage);
            self.PageInfo().StartPage(result.StartPage);
            self.PageInfo().EndPage(result.EndPage);
            self.PageInfo().NextPage(result.NextPage);
            self.PageInfo().PreviousPage(result.PreviousPage);

        });
    };

    //GetWebAppPortfolio
    self.GetWebAppPortfolio = function () {
        amplify.request("GetWebAppPortfolio", function (result) {
            self.WebAppList(result);
            if (self.WebAppList().length > 0)
            {
                self.WebAppListValue(result[0]);
            }
            self.GetNavigationAreasByPage();
        });
    };

    self.clickPagerFastBackward = function () {
        self.PageInfo().PageIndex(self.PageInfo().StartPage());
        self.GetNavigationAreasByPage();
    };
    self.clickPagerBackward = function () {
        self.PageInfo().PageIndex(self.PageInfo().PreviousPage());
        self.GetNavigationAreasByPage();
    };
    self.clickPagerForward = function () {
        self.PageInfo().PageIndex(self.PageInfo().NextPage());
        self.GetNavigationAreasByPage();
    };
    self.clickPagerFastForward = function () {
        self.PageInfo().PageIndex(self.PageInfo().EndPage());
        self.GetNavigationAreasByPage();
    };

}


$(function () {
    var vm = new DeixeiDomain.PortfolioIndexViewModel();
    ko.applyBindings(vm);

    vm.GetWebAppPortfolio();

    $("#MyForm").validate();

});

